<!-- 药品下单 -->
<template>
	<view style="box-sizing: border-box;">
		<view class="address">
			<view v-if="currentAddress.userName">
				<p>收货地址：{{currentAddress.userAddress}}</p>
				<p>收货人：{{currentAddress.userName}}</p>
			</view>
			<view v-else style="width: 100%; text-align: center; color: red;">未查到您的收货地址记录，请前往个人中心添加</view>
		</view>

		<view class="three">
			<view class="three_top">
				<text>自营</text>
			</view>
			<view class="three_center">
				<view class="thc_up">
					<view>
						<image mode="aspectFit" :src="good.item.sthumb"></image>
					</view>
					<view>
						<view class="cthc_up">
							<text>999感冒灵</text>
							<text>规格：盒</text>
						</view>
						<view class="cthc_down">
							<view>
								<text>￥<text style="font-size:36rpx">{{good.item.smoney}}</text>
								</text>
								<view class="addbox">
									<u-number-box v-model="num" @change=""></u-number-box>
								</view>
							</view>
							<text style="font-size: .6rem; color: #795548;">注：若需退货请联系平台客服</text>
						</view>
					</view>
				</view>

			</view>
		</view>

		<view class="btnpos">
			<view class="btm">
				<text>￥<text style="font-size:56rpx;">{{totalPrice}}</text>
				</text>
				<button @click="api_creatorder()">提交订单</button>
			</view>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				good: {},
				addressList: [],
				currentAddress: {
					addressId: '',
					userName: ''
				},
				num: 1,
				total: 0
			}
		},
		onLoad(option) {
			const eventChannel = this.getOpenerEventChannel();
			eventChannel.on('detail', (data) => {
				this.good = data.data
				console.log(this.good)
			})

		},
		onShow() {
			this.api_addresslist()
		},
		computed: {
			totalPrice() {
				return this.good.item.smoney * this.num
			}
		},
		methods: {


			api_addresslist() {
				let openid = uni.getStorageSync('openid')
				this.$app._get('entry/wxapp/goods.addresslist', {
					openid: wx.getStorageSync("openid"),
				}, (result) => {
					this.addressList = result
					if (this.addressList.length > 0) {
						this.currentAddress = this.addressList[0]
					}
					console.log(result)
				})
			},
			api_creatorder() {
				let that = this
				let openid = uni.getStorageSync('openid')
				if (!this.currentAddress.addressId) {
					uni.showToast({
						title: '您未选择收货地址',
						icon: 'none'
					})
					return
				}
				console.log(this.currentAddress)
				this.$app._post_form('entry/wxapp/goods.creatorder', {
					//ifcf: this.data.ifcfy,  //0非处方1处方
					//ifshop: 0, //0商城购买开方1医生和用户开方
					//conets: e, // 处方内容 json格式 传 {} 吧
					goodarr: '' + this.good.item.sid + ',', // 商品信息 json格式
					countmoney: this.good.item.smoney, //商品金额
					//feight: n, //未知，传0吧
					num: this.num, //数量
					realTotalMoney: this.good.item.smoney * this.num, //实际订单总金额 进行各种折扣之后的金额
					totalMoney: this.good.item.smoney * this.num, //订单总金额 包括运费
					deliverMoney: 0, //运费
					//u_id: c, //用户id
					addressid: this.currentAddress.addressId, //收货地址id
					openid: openid
				}, (res) => {
					console.log(res)
					let orderId = res.id
					this.$app._post_form('entry/wxapp/goods.paygoods', {
						openid: openid,
						// money: this.good.item.smoney * this.num
						money: 0.01, // 支付金额
						orderno: res.orderNo, // 订单号
					}, (res) => {
						console.log(res)
						let wx_data_package = JSON.parse(res.wx_data_package)
						let orderInfo = {
							"timeStamp": wx_data_package.timestamp,
							"nonceStr": wx_data_package.noncestr,
							"package": wx_data_package.package,
							"signType": wx_data_package.signType,
							"paySign": wx_data_package.sign,
						}
						uni.getProvider({
							service: 'payment',
							success(res) {
								console.log(res)
								if (res.provider.indexOf('wxpay') > -1) {
									console.log('开始支付')
									uni.requestPayment({
										"provider": "wxpay", //固定值为"wxpay"
										"timeStamp": orderInfo.timeStamp,
										"nonceStr": orderInfo.nonceStr,
										"package": orderInfo.package,
										"signType": orderInfo.signType,
										"paySign": orderInfo.paySign,
										success: (res) => {
											console.log(res)
											if (res.errMsg == 'requestPayment:ok') {
												console.log("支付成功");
												that.$app._post_form("entry/wxapp/tijian.paysucc", {
													openid: openid,
													id: orderId, // 药品下单接返回的id
												}, (res) => {
													uni.navigateTo({
														url: '/pages/global/successMessage?msg=下单成功，请返回首页，并前往个人中心-药品订单中查看订单详情'
													})
												})
											}
										},
										fail: (err) => {
											console.log('支付失败:' + JSON.stringify(err));
										}
									});
								}
							}
						})
					})
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.address {
		background-color: #fff;
		padding: 10px;
		font-size: .9rem;
		color: #757575;
		margin-bottom: 5px;
	}

	.three {
		width: 100%;
		padding: 24rpx;
		background: white;
		margin-top: 25rpx;
	}

	.three .three_top {
		width: 100%;
		height: 80rpx;
		display: flex;
		align-items: center;
	}

	.three .three_top text {
		font-size: 28rpx;
	}

	.three .three_center {
		width: 100%;
	}

	.three .three_center .thc_up {
		width: 100%;
		height: 220rpx;
		display: flex;
	}

	.three .three_center .thc_up>view:nth-of-type(1) {
		width: 30%;
		display: flex;
	}

	.three .three_center .thc_up>view:nth-of-type(1) image {
		margin: auto;
		height: 100px;
	}

	.three .three_center .thc_up>view:nth-of-type(2) {
		width: 70%;
		padding-left: 20rpx;
		display: flex;
		flex-direction: column;
		justify-content: space-around;
	}

	.three .three_center .thc_up>view:nth-of-type(2) .cthc_up {
		display: flex;
		flex-direction: column;
	}

	.three .three_center .thc_up>view:nth-of-type(2) text {
		white-space: nowrap;
		text-overflow: ellipsis;
		overflow: hidden;
	}

	.three .three_center .thc_up>view:nth-of-type(2) .cthc_up text:nth-of-type(1) {
		color: #333;
		font-size: 34rpx;
	}

	.three .three_center .thc_up>view:nth-of-type(2) .cthc_up text:nth-of-type(2) {
		color: #999;
		font-size: 24rpx;
	}

	.three .three_center .thc_up>view:nth-of-type(2) .cthc_down>view {
		display: flex;
		justify-content: space-between;
	}

	.three .three_center .thc_up>view:nth-of-type(2) .cthc_down>view text:nth-of-type(1) {
		color: red;
		font-weight: bold;
		font-size: 24rpx;
	}

	.three .addbox {
		padding-right: 20px;
		display: flex;
		border-radius: 10rpx;
	}

	.btnpos {
		position: fixed;
		bottom: 0px;
		width: 100%;
		padding-right: 20px;
	}

	.btm {
		width: 100%;
		height: 110rpx;
		background: #fff;
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 0px 24rpx;
		box-sizing: border-box;
	}

	.btm text {
		font-weight: bold;
		font-size: 32rpx;
		color: red;
	}

	.btm button {
		margin: 0;
		width: 250rpx;
		border-radius: 50rpx;
		background: linear-gradient(to right, #81c784, 75%, #4caf50);
		color: white;
		font-weight: bold;
		font-size: 32rpx;
	}
</style>